package com.guan.serviceUcenter.controller;


import com.guan.commonutils.JwtUtils;
import com.guan.commonutils.R;
import com.guan.commonutils.ordervo.UcenterMemberOrder;
import com.guan.serviceUcenter.entity.UcenterMember;
import com.guan.serviceUcenter.entity.vo.RegisterVo;
import com.guan.serviceUcenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-04-18
 */
@RestController
@RequestMapping("/ucenter/member/*")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;


    /**
     * @date: 2023/4/19 8:30
     * @author: 管奇安
     * @description: 登录
     * @param: [ucenterMember]
     * @return: com.guan.commonutils.R
     **/
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember ucenterMember){
        //调用方法实现登录
        //返回token值，使用jwt生成
        String token = ucenterMemberService.loginUser(ucenterMember);
        return R.ok().data("token",token);
    }

    /**
     * @date: 2023/4/19 8:56
     * @author: 管奇安
     * @description:  注册
     * @param: [registerVo]
     * @return: com.guan.commonutils.R
     **/
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){

        ucenterMemberService.register(registerVo);

        return R.ok();
    }

    /**
     * @date: 2023/4/19 9:01
     * @author: 管奇安
     * @description: 根据 token 获取用户信息
     * @param: [request]
     * @return: com.guan.commonutils.R
     **/
    @GetMapping("getUserInfo")
    public R  getUserInfo(HttpServletRequest request){

        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        UcenterMember member = ucenterMemberService.getById(memberId);

        return R.ok().data("userInfo",member);
    }


    /**
     * @date: 2023/4/24 16:58
     * @author: 管奇安
     * @description: 远程调用查询用户信息
     * @param: [id] 用户id
     * @return:
     * com.guan.commonutils.ordervo.UcenterMemberOrder
     * 为了方便调用端方便取值，返回一个对象。该对象是调用端和被调用端都可以引用，所以复制UcenterMember类
     * 到common_utils模块下。
     **/
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){

        UcenterMember memebr = ucenterMemberService.getById(id);
        //把UcenterMember对象属性值复制到UcenterMemberOrder对象中
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(memebr,ucenterMemberOrder);

        return ucenterMemberOrder;
    }


    /**
     * @date: 2023/4/25 17:03
     * @author: 管奇安
     * @description: 查询某一天的注册人数
     * @param: [day]
     * @return: com.guan.commonutils.R
     **/
    @GetMapping(value = "countregister/{day}")
    public R registerCount(
            @PathVariable String day){
        Integer count = ucenterMemberService.countRegisterByDay(day);
        return R.ok().data("countRegister", count);
    }



}

